Set ANSI_NULLS ON
Set QUOTED_IDENTIFIER ON
GO

/*Drop stored procedures if they exists */

IF EXISTS (SELECT name FROM sysobjects WHERE name = 'eliminarBodega' and type = 'P')

	BEGIN
		DROP PROCEDURE eliminarBodega
	END

GO

CREATE PROCEDURE eliminarBodega ( @bodegaObjetivo INT ,
								  @bodegaReemplazo INT , 	 
								  @outParam INT OUTPUT						
								)
AS
	DECLARE @out AS INT;
	SET @out = -1;
	
	BEGIN TRANSACTION;
	
		BEGIN TRY
		
			UPDATE MATERIAL_REAL SET MATERIAL_REAL.IDBODEGA = @bodegaReemplazo
			WHERE MATERIAL_REAL.IDBODEGA = @bodegaObjetivo;
			
			DELETE FROM BODEGA WHERE BODEGA.IDBODEGA = @bodegaObjetivo;
			
			SET @out = 1;
			
			SET @outParam = @out;
			
			COMMIT TRANSACTION; 
		
		END TRY
		
		BEGIN CATCH
		
			SET @outParam = @out;
			
			ROLLBACK TRANSACTION;
		
		END CATCH;
	
GO